 //
//  DiseaseForMaintainDetailVC.m
//  RoadMaintain
//
//  Created by 岳学坤 on 2017/3/17.
//  Copyright © 2017年 岳学坤. All rights reserved.
//

#import "DiseaseForMaintainDetailVC.h"
#import "PutNormolForIPhoneCell.h"
#import "SearchForDiseaseAddPicsForIPhoneCell.h"
#import "SearchForDiseaseForIPhoneCell.h"
#import "PutUpFileForIPhoneCell.h"
#define PutCellID @"cellid1"
#define PicCellID @"cellid2"
#define DetailCellID @"cellid3"
#define PutFileCellID @"cellid4"
#define kAlphaNum @".0123456789"

@interface DiseaseForMaintainDetailVC ()<DetailForIPhoneCellDelegate,PicsForIPhoneDelegate,UIAlertViewDelegate>
{
    UIButton * saveBtn;
    UIButton * submit;
    UIButton * safeBtn;
    NSString * _status;//0 保存  1  上报
    UIAlertView *startAlert;
}
@property (nonatomic,strong) NSDictionary *dic;//获取的需要展示的ID字典
@property (nonatomic,strong) UIView *footerView;
@property (nonatomic,strong) NSArray *titleArray;
@property (nonatomic,strong) NSArray *contentArray;
@property (nonatomic,strong) NSMutableArray *noticeArray;

@property (nonatomic,strong) NSMutableArray *weatherArray;
@property (nonatomic,strong) NSString *weatherId;
@property (nonatomic,strong) NSString *weatherName;
@property (nonatomic,strong) NSString *startTime;
@property (nonatomic,strong) NSString *endTime;
@property (nonatomic,strong) NSMutableArray *imgArray;
@property (nonatomic,strong) NSString *detail;
@property (nonatomic,strong) NSString *directorName;
@property (nonatomic,strong) NSString *directorId;
@property (nonatomic,strong) NSString *checkerName;
@property (nonatomic,strong) NSString *checkerId;
@property (nonatomic,strong) NSString *num;
@property (nonatomic,strong) NSString *divideTypeName;
@property (nonatomic,strong) NSString *divideTypeId;
@property (nonatomic,strong) NSString *measureUnitCode;
@property (nonatomic,strong) NSString *maxPrice;
@property (nonatomic,strong) NSString *price;
@property (nonatomic,strong) NSString *subProjectTypeCode;
@property (nonatomic,strong) NSString *subProjectTypeName;
@property (nonatomic,strong) NSString *maintenanceMeasureId;
@property (nonatomic,strong) NSString *maintenanceMeasureCode;
@property (nonatomic,strong) NSString *measureName;
@property (nonatomic,strong) NSString *measureNo;



@property (nonatomic,strong) NSMutableArray * deleteArray;//删除的图片数组
@end

@implementation DiseaseForMaintainDetailVC

#pragma mark - Life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self resetNav];
    [self setWebData];
    [self setTableViewFooterView];
}
#pragma mark - Property
- (NSArray *)titleArray{
    if (_titleArray==nil) {
        _titleArray = @[@"维修记录编码",@"病害巡查编码",@"通知单编码",@"损坏类型",@"维修部门",@"维修开始日期",@"维修结束日期",@"天气",@"分项工程类型",@"项目工程编码",@"细目编码",@"细目名称",@"细目控制价(元)",@"计量单位",@"单价(元)",@"实际数量",@"检查人",@"负责人",@"现场维修图片",@"备注"];
    }
    return _titleArray;
}
- (NSMutableArray *)noticeArray{
    if (_noticeArray == nil) {
        _noticeArray = [[NSMutableArray alloc]initWithCapacity:1];
    }
    return _noticeArray;
}
- (NSMutableArray *)weatherArray{
    if (_weatherArray == nil) {
        _weatherArray = [[NSMutableArray alloc]initWithCapacity:1];
    }
    return _weatherArray;
}
- (NSMutableArray *)imgArray{
    if (_imgArray==nil) {
        _imgArray = [[NSMutableArray alloc]initWithCapacity:1];
    }
    return _imgArray;
}
- (NSMutableArray *)deleteArray{
    if (_deleteArray == nil) {
        _deleteArray = [[NSMutableArray alloc]initWithCapacity:1];
    }
    return _deleteArray;
}
#pragma mark - UI
- (void)resetNav{
    [self setTheBackBtn];
    self.myTitle.text = @"维修记录";
    [self.tableView registerClass:[PutNormolForIPhoneCell class] forCellReuseIdentifier:PutCellID];
    [self.tableView registerClass:[SearchForDiseaseAddPicsForIPhoneCell class] forCellReuseIdentifier:PicCellID];
    [self.tableView registerClass:[SearchForDiseaseForIPhoneCell class] forCellReuseIdentifier:DetailCellID];
    [self.tableView registerClass:[PutUpFileForIPhoneCell class] forCellReuseIdentifier:PutFileCellID];
}
- (void)setTableViewFooterView{
    
    self.footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 200*WidthRate)];
    self.footerView.backgroundColor = [PublicMethod colorWithHexString:@"#eaeaea" andAlpha:1];
    self.tableView.tableFooterView = self.footerView;
    
    saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    saveBtn.layer.cornerRadius = 5;
    saveBtn.layer.masksToBounds = YES;
    saveBtn.layer.borderColor = [MAIN_Color CGColor];
    saveBtn.layer.borderWidth = 1;
    saveBtn.backgroundColor = Bai_Color;
    [saveBtn setTitleColor:MAIN_Color forState:UIControlStateNormal];
    [saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    [saveBtn addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];
    saveBtn.titleLabel.font = [UIFont systemFontOfSize:17*ZitiRate];
    [self.footerView addSubview:saveBtn];
    [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-120*WidthRate);
        make.height.mas_equalTo(42*WidthRate);
        make.left.mas_equalTo(22*WidthRate);
        make.right.equalTo(self.footerView.mas_centerX).offset(-15*WidthRate);
    }];
    
    
    submit = [UIButton buttonWithType:UIButtonTypeCustom];
    submit.layer.cornerRadius = 5;
    submit.layer.masksToBounds = YES;
    submit.backgroundColor = MAIN_Color;
    [submit setTitleColor:Bai_Color forState:UIControlStateNormal];
    [submit setTitle:@"上报" forState:UIControlStateNormal];
    [submit addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];
    submit.titleLabel.font = [UIFont systemFontOfSize:17*ZitiRate];
    [self.footerView addSubview:submit];
    [submit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-120*WidthRate);
        make.height.mas_equalTo(42*WidthRate);
        make.right.mas_equalTo(-22*WidthRate);
        make.left.equalTo(self.footerView.mas_centerX).offset(15*WidthRate);
    }];
    
    safeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    safeBtn.layer.cornerRadius = 5;
    safeBtn.layer.masksToBounds = YES;
    safeBtn.backgroundColor = MAIN_Color;
    [safeBtn setTitleColor:Bai_Color forState:UIControlStateNormal];
    [safeBtn setTitle:@"安全登记" forState:UIControlStateNormal];
    [safeBtn addTarget:self action:@selector(safeClick) forControlEvents:UIControlEventTouchUpInside];
    safeBtn.titleLabel.font = [UIFont systemFontOfSize:17*ZitiRate];
    [self.footerView addSubview:safeBtn];
    [safeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-120*WidthRate);
        make.height.mas_equalTo(46*WidthRate);
        make.left.mas_equalTo(25*WidthRate);
        make.right.mas_equalTo(-25*WidthRate);
    }];
    
    if (self.state==2) {
        self.tableView.tableFooterView.hidden = YES;

    }else if (self.state==1){
        self.tableView.tableFooterView.hidden = NO;
        saveBtn.hidden = YES;
        submit.hidden = YES;
        safeBtn.hidden = NO;
    }else{
        self.tableView.tableFooterView.hidden = NO;
        saveBtn.hidden = NO;
        submit.hidden = NO;
        safeBtn.hidden = YES;
    }
}

#pragma mark - UI Response
- (void)save{
    _status = @"0";
    [self putData];
}
- (void)submit{
    _status = @"1";
    [self putData];
}
- (void)putData{
     if([self.weatherId length]==0){
        [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"天气不能为空。" animated:YES];
     }else if([self.subProjectTypeCode length]==0){
         [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"分项工程类型为空。" animated:YES];
     }else if([self.maintenanceMeasureId length]==0){
         [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"项目工程编码不能为空。" animated:YES];
     }else{
        if ([_status integerValue]==1) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"上报" message:@"确定要上报此维修单吗？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        }else{
            [self endPutData];
        }
        
    }
    
}
- (void)endPutData{
    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    NSMutableArray * addImgArray = [[NSMutableArray alloc]initWithCapacity:1];
    for (int i = 0; i<self.imgArray.count; i++) {
        if ([self.imgArray[i] isKindOfClass:[UIImage class]]) {
            [addImgArray addObject:self.imgArray[i]];
        }
    }
    [NetWorkTool putUpDiseaseMaintain:self.dic[@"maintentanceRecordId"] maintainUnitId:self.dic[@"maintainUnitId"]  maintentanceNoticeNo:self.dic[@"maintentanceNoticeNo"] maintenanceDepartmentId:self.dic[@"maintenanceDepartmentId"] startDate:self.startTime endDate:self.endTime checkUser:self.checkerId directorUser:self.directorId remark:self.detail weather:self.weatherId state:_status subProject:self.subProjectTypeCode measureCode:self.maintenanceMeasureId maxPrice:self.maxPrice unit:self.measureUnitCode price:self.price num:self.num block:^(id data) {
      
        if (data) {
            if ([data[@"success"]integerValue]) {
                
                [NetWorkTool userHeadImg:addImgArray deleteArr:self.deleteArray billId:self.dic[@"maintentanceRecordId"] billType:@"dmMaintentanceRecord" success:^(NSDictionary *responseObject) {
                     [MBProgressHUD hideAllHUDsForView:self.tableView animated:NO];
                    if ([_status integerValue]==0) {
                        [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"保存成功" animated:YES];
                    }else{
                        [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"上报成功" animated:YES];
                    }
                    [self performSelector:@selector(pop) withObject:nil afterDelay:1];
                } failure:^(NSError *error) {
                      [MBProgressHUD hideAllHUDsForView:self.tableView animated:NO];
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"网络错误，操作失败。" animated:YES];
                } uploadProgressBlock:^(float uploadPercent, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
                    
                }];
                
            }else{
                  [MBProgressHUD hideAllHUDsForView:self.tableView animated:NO];
                if ([data[@"message"] isEqualToString:@"请求需要进行验证,请重新传入Token"]) {
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"账号被其他设备登录，请重新登录" animated:YES];
                    [self performSelector:@selector(gotoLogin) withObject:nil afterDelay:1];
                }else{
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:data[@"message"] animated:YES];
                }
            }
        }else{
            [MBProgressHUD hideAllHUDsForView:self.tableView animated:NO];
            [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"网络错误，操作失败。" animated:YES];
        }
    }];
    
}

- (void)pop{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)safeClick{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"确认" message:@"确定要安全登记吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
    
}

#pragma mark - Data handle
- (void)setWebData{
    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    [NetWorkTool getDiseaseMaintainDetailWithID:self.codeId block:^(id data) {
        [MBProgressHUD hideAllHUDsForView:self.tableView animated:NO];
        if (data) {
            if ([data[@"success"] integerValue]) {
                self.dic = data[@"data"][@"DmMaintentanceRecordList"];
                self.startTime = self.dic[@"startDate"];
                self.endTime = self.dic[@"endDate"];
                [self.weatherArray removeAllObjects];
                [self.weatherArray addObjectsFromArray: data[@"data"][@"DictWeatherCodeList"]];
                [self.noticeArray removeAllObjects];
                [self.noticeArray addObjectsFromArray:data[@"data"][@"noticeLists"]];
                self.weatherId = self.dic[@"weather"];
                self.weatherName = self.dic[@"weatherName"];
                self.detail = self.dic[@"remark"];
                self.num = [NSString stringWithFormat:@"%@",self.dic[@"actualNum"]];
                self.subProjectTypeCode = self.dic[@"subProjectTypeCode"];
                self.subProjectTypeName = self.dic[@"subProjectTypeName"];
                self.maintenanceMeasureCode = self.dic[@"maintenanceMeasureCode"];
                self.maintenanceMeasureId = self.dic[@"maintenanceMeasureId"];
                self.measureName = self.dic[@"measureName"];
                self.measureNo = self.dic[@"measureNo"];
                self.measureUnitCode = self.dic[@"measureUnitCode"];
                self.maxPrice = [NSString stringWithFormat:@"%@",self.dic[@"maxPrice"]];
                self.price = [NSString stringWithFormat:@"%@",self.dic[@"price"]];
                
                self.directorName = self.dic[@"directorUserName"];
                self.directorId = self.dic[@"directorUser"];
                self.checkerId = self.dic[@"checkerUser"];;
                self.checkerName = self.dic[@"checkerUserName"];
                
                [self.imgArray removeAllObjects];
                if ([self.dic[@"filePath"] length]==0) {
                    
                }else{
                    [self.imgArray addObjectsFromArray: [self.dic[@"filePath"] componentsSeparatedByString:@","]];
                }

                [self.tableView reloadData];
            }else{
                if ([data[@"message"] isEqualToString:@"请求需要进行验证,请重新传入Token"]) {
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"账号被其他设备登录，请重新登录" animated:YES];
                    [self performSelector:@selector(gotoLogin) withObject:nil afterDelay:1];
                }else{
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:data[@"message"] animated:YES];
                }            }
        }else{
            [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"网络错误，请检查网络后重试。" animated:YES];
        }
    }];
}
- (void)gotoLogin{
    [PublicMethod noTokenToLoginWithNav:self.navigationController];
}
#pragma mark - Delegate
- (void)picsForIphoneWithImgArray:(NSMutableArray *)arr AndDeleteArray:(NSMutableArray *)deleteArray{
    [self.imgArray removeAllObjects];
    [self.imgArray addObjectsFromArray:arr];
    [self.deleteArray removeAllObjects];
    [self.deleteArray addObjectsFromArray:deleteArray];
}
- (void)detailForIPhoneCellWithString:(NSString *)str indexPath:(NSIndexPath *)indexPath{
    self.detail = str;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if ([self.imgArray count]==0&&self.state==1) {
        return 2;
    }else{
        return 3;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return 18;
            break;
        case 1:
            return 1;
            break;
        default:
            return 1;
            break;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return 45*WidthRate;
    }else{
        return 135*WidthRate;
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.state==0) {
        if (indexPath.row==7) {
            UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"天气" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
            for (int i = 0; i<self.weatherArray.count; i++) {
                [actionSheet addAction:[UIAlertAction actionWithTitle:self.weatherArray[i][@"itemName"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    self.weatherName = self.weatherArray[i][@"itemName"];
                    self.weatherId = self.weatherArray[i][@"itemCode"];
                    PutNormolForIPhoneCell *cell = (PutNormolForIPhoneCell *)[self.tableView cellForRowAtIndexPath:indexPath];
                    cell.contentLab.text = self.weatherName;
                }]];
            }
            [actionSheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self presentViewController:actionSheet animated:YES completion:nil];
            });
        }
        if(indexPath.row ==5){
            //x,y 值无效，默认是居中的
            KSDatePicker* picker = [[KSDatePicker alloc] initWithFrame:CGRectMake(0, 0, 280, 260)];
            
            //配置中心，详情见KSDatePikcerApperance
            
            picker.appearance.radius = 5;
            
            //设置回调
            picker.appearance.resultCallBack = ^void(KSDatePicker* datePicker,NSDate* currentDate,KSDatePickerButtonType buttonType){
                
                if (buttonType == KSDatePickerButtonCommit) {
                    
                    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
                    [formatter setDateFormat:@"yyyy-MM-dd"];
                    self.startTime = [formatter stringFromDate:currentDate];
                    PutNormolForIPhoneCell *cell = (PutNormolForIPhoneCell *)[self.tableView cellForRowAtIndexPath:indexPath];
                    cell.contentLab.text = self.startTime;
                }
            };
            // 显示
            [picker show];
        }
        if (indexPath.row == 6) {
            //x,y 值无效，默认是居中的
            KSDatePicker* picker = [[KSDatePicker alloc] initWithFrame:CGRectMake(0, 0, 280, 260)];
            
            //配置中心，详情见KSDatePikcerApperance
            
            picker.appearance.radius = 5;
            
            //设置回调
            picker.appearance.resultCallBack = ^void(KSDatePicker* datePicker,NSDate* currentDate,KSDatePickerButtonType buttonType){
                
                if (buttonType == KSDatePickerButtonCommit) {
                    
                    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
                    [formatter setDateFormat:@"yyyy-MM-dd"];
                    self.endTime = [formatter stringFromDate:currentDate];
                    PutNormolForIPhoneCell *cell = (PutNormolForIPhoneCell *)[self.tableView cellForRowAtIndexPath:indexPath];
                    cell.contentLab.text = self.endTime;
                }
            };
            // 显示
            [picker show];
        }
        if (indexPath.row==8) {
            UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"分项工程类型" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
            for (int i = 0; i<self.noticeArray.count; i++) {
                [actionSheet addAction:[UIAlertAction actionWithTitle:self.noticeArray[i][@"subProjectTypeName"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    self.subProjectTypeName = self.noticeArray[i][@"subProjectTypeName"];
                    self.subProjectTypeCode = self.noticeArray[i][@"subProjectTypeCode"];
                    self.maintenanceMeasureCode = nil;
                    self.maintenanceMeasureId = nil;
                    self.measureNo = nil;
                    self.measureName = nil;
                    self.maxPrice = nil;
                    self.measureUnitCode = nil;
                    self.price = nil;
                    [self.tableView reloadData];
                    
                }]];
            }
            [actionSheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self presentViewController:actionSheet animated:YES completion:nil];
            });

        }
        if (indexPath.row==9) {
            if (self.subProjectTypeCode) {
                [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
                [NetWorkTool getProjectCodeWithSubProjectTypeCode:self.subProjectTypeCode block:^(id data) {
                   [MBProgressHUD hideAllHUDsForView:self.tableView animated:NO];
                    if (data) {
                        if([data[@"success"] integerValue]){
                            UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"项目工程编码" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
                            for (int i = 0; i<[data[@"data"] count]; i++) {
                                [actionSheet addAction:[UIAlertAction actionWithTitle:[NSString stringWithFormat:@"%@",data[@"data"][i][@"maintenanceMeasureCode"]]style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                                    self.maintenanceMeasureCode = data[@"data"][i][@"maintenanceMeasureCode"];
                                    self.maintenanceMeasureId = data[@"data"][i][@"maintenanceMeasureId"];
                                    self.measureName = data[@"data"][i][@"measureName"];
                                    self.measureNo = data[@"data"][i][@"measureNo"];
                                    self.maxPrice = data[@"data"][i][@"maxPrice"];
                                    self.measureUnitCode = data[@"data"][i][@"measureUnitCode"];
                                    self.price = data[@"data"][i][@"price"];
                                    [self.tableView reloadData];
                                }]];
                            }
                            [actionSheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
                            
                            dispatch_async(dispatch_get_main_queue(), ^{
                                [self presentViewController:actionSheet animated:YES completion:nil];
                            });
                            
                        }else{
                            
                            [PublicMethod noTokenToLoginWithNav:self.navigationController];
                        }
                        
                    }else{
                        [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"因网络问题获取项目工程编码失败" animated:YES];
                    }
                    
                }];
                
                
            }else{
                [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"请先选择分项工程类型" animated:YES];
                
            }
        }
        if (indexPath.row == 16) {
            [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
            [NetWorkTool receiveManWithUnitID:nil block:^(id data) {
               [MBProgressHUD hideAllHUDsForView:self.tableView animated:NO];
                if (data) {
                    if([data[@"success"] integerValue]){
                        UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"检查人" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
                        for (int i = 0; i<[data[@"data"] count]; i++) {
                            [actionSheet addAction:[UIAlertAction actionWithTitle:[NSString stringWithFormat:@"%@",data[@"data"][i][@"userName"]]style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                                self.checkerName = data[@"data"][i][@"userName"];
                                self.checkerId = data[@"data"][i][@"userId"];
                                PutNormolForIPhoneCell *cell = (PutNormolForIPhoneCell *)[self.tableView cellForRowAtIndexPath:indexPath];
                                cell.contentLab.text = self.checkerName;
                                
                            }]];
                        }
                        [actionSheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
                        
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [self presentViewController:actionSheet animated:YES completion:nil];
                        });
                        
                    }else{
                        
                        [PublicMethod noTokenToLoginWithNav:self.navigationController];
                    }
                    
                }else{
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"因网络问题获取检查人失败" animated:YES];
                }
                
            }];

        }
        if (indexPath.row == 17) {
            [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
            [NetWorkTool receiveManWithUnitID:nil block:^(id data) {
            [MBProgressHUD hideAllHUDsForView:self.tableView animated:NO];
                if (data) {
                    if([data[@"success"] integerValue]){
                        UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"负责人" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
                        for (int i = 0; i<[data[@"data"] count]; i++) {
                            [actionSheet addAction:[UIAlertAction actionWithTitle:[NSString stringWithFormat:@"%@",data[@"data"][i][@"userName"]]style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                                self.directorName = data[@"data"][i][@"userName"];
                                self.directorId = data[@"data"][i][@"userId"];
                                PutNormolForIPhoneCell *cell = (PutNormolForIPhoneCell *)[self.tableView cellForRowAtIndexPath:indexPath];
                                cell.contentLab.text = self.directorName;
                                
                            }]];
                        }
                        [actionSheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
                        
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [self presentViewController:actionSheet animated:YES completion:nil];
                        });
                        
                    }else{
                        
                        [PublicMethod noTokenToLoginWithNav:self.navigationController];
                    }
                    
                }else{
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"因网络问题获取负责人失败" animated:YES];
                }
                
            }];
            
        }
        if (indexPath.row == 15) {
            startAlert = [[UIAlertView alloc]initWithTitle:@"请输入实际数量" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [startAlert setAlertViewStyle:UIAlertViewStylePlainTextInput];
            UITextField *startField = [startAlert textFieldAtIndex:0];
            startField.placeholder = @"请输入实际数量";
            startField.keyboardType = UIKeyboardTypeDecimalPad;
            if ([self.num doubleValue]==0) {
                startField.text = nil;
            }else{
                startField.text = self.num;
            }
            [startField becomeFirstResponder];
            [startAlert show];
        }

    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
    
        PutNormolForIPhoneCell *cell = [tableView dequeueReusableCellWithIdentifier:PutCellID];
        cell.titleLab.text = self.titleArray[indexPath.row];
            switch (indexPath.row) {
                case 0:
                {
                   cell.contentLab.text = self.dic[@"billNo"];
                }
                    break;
                case 1:
                {
                     cell.contentLab.text = self.dic[@"diseaseInspectionNo"];
                }
                    break;
                case 2:
                {
                    cell.contentLab.text = self.dic[@"maintentanceNoticeNo"];
                  
                }
                    break;
                case 3:
                {
                    cell.contentLab.text = self.dic[@"diseaseTypeName"];
                }
                    break;
                case 4:
                {
                      cell.contentLab.text = self.dic[@"maintenanceDepartment"];
                }
                    break;
                case 5:
                {
                    cell.contentLab.text = self.startTime;
                }
                    break;
                case 6:
                {
                    cell.contentLab.text = self.endTime;
                }
                    break;
                case 7:
                {
                    cell.contentLab.text = self.weatherName;
                }
                    break;
                case 8:
                {
                    cell.contentLab.text = self.subProjectTypeName;
                }
                    break;
                case 9:
                {
                    cell.contentLab.text = self.maintenanceMeasureCode;
                }
                    break;
                case 10:
                {
                    cell.contentLab.text = self.measureNo;
                }
                    break;
                case 11:
                {
                    cell.contentLab.text = self.measureName
                    ;
                }
                    break;
                case 12:
                {
                    
                    cell.contentLab.text = [PublicMethod changeFloat:[NSString stringWithFormat:@"%.6f",[self.maxPrice doubleValue]]];
                    [cell.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
                        make.left.mas_equalTo(10*WidthRate);
                        make.top.mas_equalTo(0);
                        make.bottom.mas_equalTo(0);
                        make.width.mas_equalTo(130*WidthRate);
                    }];
                }
                    break;
                case 13:
                {
                    cell.contentLab.text = self.measureUnitCode;
                }
                    break;
                case 14:
                {
                   cell.contentLab.text = [PublicMethod changeFloat:[NSString stringWithFormat:@"%.6f",[self.price doubleValue]]];
                }
                    break;
                case 15:
                {
                    cell.contentLab.text = [PublicMethod changeFloat:[NSString stringWithFormat:@"%.6f",[self.num doubleValue]]];
                }
                    break;
                case 16:
                {
                    cell.contentLab.text = self.checkerName;
                }
                    break;
                case 17:
                {
                    cell.contentLab.text = self.directorName;
                }
                    break;
                default:
                    break;
        }
        if (self.state ==1 ||self.state ==2) {
                cell.img.hidden = YES;
            }else{
        if (indexPath.row==5||indexPath.row==6||indexPath.row==7||indexPath.row==17||indexPath.row==16||indexPath.row==15||indexPath.row==8||indexPath.row==9) {
            cell.img.hidden = NO;
            cell.img.image = [UIImage imageNamed:@"enter"];
        }else {
            cell.img.hidden = YES;
        }
            }
        return  cell;
    } else if(indexPath.section==1){
         if (self.state == 1&&[self.imgArray count]==0) {
             
             SearchForDiseaseForIPhoneCell *cell = [tableView dequeueReusableCellWithIdentifier:DetailCellID];
             cell.delegate = self;
             cell.indexPath = indexPath;
             cell.titleLab.text = @"备注";
             if (self.state == 1 ||self.state==2) {
                 cell.detailTextView.editable = NO;
                 if ([self.detail length]==0) {
                     cell.detailTextView.text = @" ";
                 }else{
                     cell.detailTextView.text= self.detail;
                 }
             }else{
                 cell.detailTextView.editable = YES;
                 cell.detailTextView.placeholder = @"请输入备注内容";
                 cell.detailTextView.text = self.detail;
             }
             return  cell;
         }else{
        SearchForDiseaseAddPicsForIPhoneCell *cell = [tableView dequeueReusableCellWithIdentifier:PicCellID];
             cell.titleLab.text = @"现场维修图片";
             cell.delegate = self;
             if (self.state==1 ||self.state == 2) {
                 cell.isReset = NO;
             }else{
                 cell.isReset = YES;
             }
             cell.con = self;
             [cell addValue:self.imgArray];
        return  cell;
         }
    }else{
        SearchForDiseaseForIPhoneCell *cell = [tableView dequeueReusableCellWithIdentifier:DetailCellID];
        cell.delegate = self;
        cell.indexPath = indexPath;
        cell.titleLab.text = @"备注";
        if (self.state == 1||self.state==2) {
            cell.detailTextView.editable = NO;
            if ([self.detail length]==0) {
                cell.detailTextView.text = @" ";
            }else{
                cell.detailTextView.text= self.detail;
            }
        }else{
            cell.detailTextView.editable = YES;
            cell.detailTextView.placeholder = @"请输入备注内容";
            cell.detailTextView.text = self.detail;
        }
        
        return  cell;
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if ([alertView.title isEqualToString:@"上报"]) {
        if (buttonIndex) {
            [self endPutData];
        }
    }else if([alertView.title isEqualToString:@"确认"]){
        [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
        
        [NetWorkTool securityRegisterWithMaintentanceRecordId:self.dic[@"maintentanceRecordId"] block:^(id data) {
            [MBProgressHUD hideAllHUDsForView:self.tableView animated:NO];
            if (data) {
                if ([data[@"success"]integerValue]) {
                    
                    
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"安全登记成功" animated:YES];
                    
                    [self performSelector:@selector(pop) withObject:nil afterDelay:1];
                    
                    
                }else{
                    if ([data[@"message"] isEqualToString:@"请求需要进行验证,请重新传入Token"]) {
                        [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"账号被其他设备登录，请重新登录" animated:YES];
                        [self performSelector:@selector(gotoLogin) withObject:nil afterDelay:1];
                    }else{
                        [MBProgressHUD showTextHUDAddedTo:self.view Msg:data[@"message"] animated:YES];
                    }
                }
            }else{
                
                [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"网络错误，操作失败。" animated:YES];
            }
        }];

    }else  {
        
    if (buttonIndex == alertView.firstOtherButtonIndex) {
            UITextField* textField = [alertView textFieldAtIndex:0];
            if([textField.text removeSpace].length==0){
                [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"输入数量为空" animated:YES];
                return;
            }else{
                NSCharacterSet *cs;
                cs = [[NSCharacterSet characterSetWithCharactersInString:kAlphaNum] invertedSet];
                
                NSString *filtered = [[textField.text componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""]; //按cs分离出数组,数组按@""分离出字符串
                self.num =[PublicMethod changeFloat:[NSString stringWithFormat:@"%f",[filtered doubleValue]]];
                
                PutNormolForIPhoneCell *cell = (PutNormolForIPhoneCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:15 inSection:0]];
                cell.contentLab.text = self.num;
            }
        }
    }
}
@end
